package com.ruoyi.crm.strategy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 图片存储策略上下文
 * 
 * @author ruoyi
 * @date 2025-11-03
 */
@Component
public class ImageStorageContext {
    
    @Value("${image.storage.type:local}")
    private String storageType;
    
    private final Map<String, ImageStorageStrategy> strategies = new ConcurrentHashMap<>();
    
    @Autowired
    public ImageStorageContext(Map<String, ImageStorageStrategy> strategyMap) {
        for (ImageStorageStrategy strategy : strategyMap.values()) {
            strategies.put(strategy.getStrategyName(), strategy);
        }
    }
    
    /**
     * 获取当前配置的存储策略
     * 
     * @return 存储策略
     */
    public ImageStorageStrategy getCurrentStrategy() {
        ImageStorageStrategy strategy = strategies.get(storageType);
        if (strategy == null) {
            // 默认使用本地存储
            strategy = strategies.get("local");
        }
        return strategy;
    }
    
    /**
     * 根据策略名称获取存储策略
     * 
     * @param strategyName 策略名称
     * @return 存储策略
     */
    public ImageStorageStrategy getStrategy(String strategyName) {
        return strategies.get(strategyName);
    }
    
    /**
     * 获取所有可用的存储策略
     * 
     * @return 存储策略映射
     */
    public Map<String, ImageStorageStrategy> getAllStrategies() {
        return strategies;
    }
}